﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _4_02_文件及文件夹操作
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                this.textBox1.Text = openFileDialog.FileName;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog foldBrowerDialog = new FolderBrowserDialog();
            if (foldBrowerDialog.ShowDialog() == DialogResult.OK)
            {
                this.textBox2.Text = foldBrowerDialog.SelectedPath;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (!File.Exists(this.textBox1.Text))
            {
                MessageBox.Show("源文件不存在！");
                return;
            }
            if (!Directory.Exists(this.textBox2.Text))
            {
                MessageBox.Show("文件夹不存在！");
                return;
            }
            try
            {
                string[] FileName = this.textBox1.Text.Split('\\');
                //参数三为覆盖选项
                string Path = this.textBox2.Text + '\\' + FileName[FileName.Length - 1];
                //File.Copy(this.textBox1.Text, this.textBox2.Text + '\\' + FileName[FileName.Length - 1], true);
                if (File.Exists(Path))
                {
                    DialogResult dr = MessageBox.Show("目标文件已存在,是否进行覆盖?", "提示:", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (dr == DialogResult.Yes)
                    {
                        File.Copy(this.textBox1.Text, this.textBox2.Text + '\\' + FileName[FileName.Length - 1], true);
                        MessageBox.Show("复制成功");
                        return;
                    }
                    else
                    {
                        return;
                    }
                }
                File.Copy(this.textBox1.Text, this.textBox2.Text + '\\' + FileName[FileName.Length - 1], true);
                MessageBox.Show("复制成功");
                return;


            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }

        }

        private void button4_Click(object sender, EventArgs e)
        {

            if (!File.Exists(this.textBox1.Text))
            {
                MessageBox.Show("源文件不存在！");
                return;
            }
            if (!Directory.Exists(this.textBox2.Text))
            {
                MessageBox.Show("文件夹不存在！");
                return;
            }

            try
            {
                string[] FileName = this.textBox1.Text.Split('\\');
                string Path = this.textBox2.Text + '\\' + FileName[FileName.Length - 1];
                //File.Move(this.textBox1.Text, Path);
                if (File.Exists(Path))
                {
                    DialogResult dr = MessageBox.Show("目标文件已存在,是否继续覆盖", "提示: ", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (dr == DialogResult.Yes)
                    {
                        File.Delete(Path);
                        File.Move(this.textBox1.Text, Path);
                        MessageBox.Show("移动成功!");
                        return;
                    }
                    else
                    {
                        return;
                    }
                }
                File.Move(this.textBox1.Text, Path);
                MessageBox.Show("移动成功");
            }
            catch (Exception ex)
            {

                throw;
            }

        }

        private void button5_Click(object sender, EventArgs e)
        {

            if (!File.Exists(this.textBox1.Text))
            {
                MessageBox.Show("源文件不存在！");
                return;
            }
            File.Delete(this.textBox1.Text);
            MessageBox.Show("删除成功！");
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
